package com.qf.controller;

import com.qf.entity.User;
import com.qf.entity.base.ResultEntity;
import com.qf.entity.netty.ShutDownMsg;
import com.qf.service.IUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/14
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IUserService userService;

    @RequestMapping("/register")
    public ResultEntity register(User user){
        System.out.println("user = " + user);
        Integer i = userService.register(user);
        if (i==1){
            return ResultEntity.error("用户名已存在");
        } else if (i==2){
            return ResultEntity.error("手机号已存在");
        }
        return ResultEntity.successs();
    }

    /**
     *
     * @param username
     * @param password
     * @param did   设备id
     * @return
     */
    @RequestMapping("/login")
    public ResultEntity login(String username,String password,String did){
        User user = userService.getUserByName(username);
        if(user!=null && user.getPassword().equals(password)){
            user.setPassword(null);

            //先根据用户id查询did
            String redisDid = redisTemplate.opsForValue().get(user.getId().toString());
            if(redisDid!=null && !redisDid.equals(did)){
                //用户挤下线
                ShutDownMsg shutDownMsg = new ShutDownMsg();
                shutDownMsg.setDid(redisDid);
                rabbitTemplate.convertAndSend("wx_exchange","",shutDownMsg);

            }

            //登录成功后要保持用户id和设备id
            redisTemplate.opsForValue().set(user.getId().toString(),did);

            return ResultEntity.successs(user);
        } else {
            return ResultEntity.error("用户名或密码错误");
        }
    }

    @RequestMapping("/getUserByName")
    public ResultEntity getUserByName(String username){
        User user = userService.getUserByName(username);
        return ResultEntity.successs(user);
    }

    /**
     * 供其他微服务调用
     * @param username
     * @return
     */
    @RequestMapping("/findUserByName")
    public User findUserByName(String username){
        System.out.println("username = " + username);
        if(!StringUtils.isEmpty(username)){
            User user = userService.getUserByName(username);
            return user;
        }
        return null;
    }

    @RequestMapping("/getUserById")
    public User getUserById(Integer id){
        System.out.println("id = " + id);
        if(id!=null){
            return userService.selectById(id);
        }
        return null;
    }

    @RequestMapping("/findUserById")
    public ResultEntity findUserById(Integer id){
        User user = userService.selectById(id);
        return ResultEntity.successs(user);
    }
}
